package com.philips.work.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.philips.work.mapper.YwSupplierMapper;
import com.philips.work.domain.YwSupplier;
import com.philips.work.service.IYwSupplierService;

/**
 * 供应商Service业务层处理
 * 
 * @author philips
 * @date 2025-05-22
 */
@Service
public class YwSupplierServiceImpl implements IYwSupplierService 
{
    @Autowired
    private YwSupplierMapper ywSupplierMapper;

    /**
     * 查询供应商
     * 
     * @param ids 供应商主键
     * @return 供应商
     */
    @Override
    public YwSupplier selectYwSupplierByIds(Long ids)
    {
        return ywSupplierMapper.selectYwSupplierByIds(ids);
    }

    /**
     * 查询供应商列表
     * 
     * @param ywSupplier 供应商
     * @return 供应商
     */
    @Override
    public List<YwSupplier> selectYwSupplierList(YwSupplier ywSupplier)
    {
        return ywSupplierMapper.selectYwSupplierList(ywSupplier);
    }

    /**
     * 查询所有供应商列表
     *
     * @return 供应商列表
     */
    @Override
    public List<YwSupplier> findAllSupplierList() {
        YwSupplier ywSupplier = new YwSupplier();
        return ywSupplierMapper.selectYwSupplierList(ywSupplier);
    }

    /**
     * 新增供应商
     * 
     * @param ywSupplier 供应商
     * @return 结果
     */
    @Override
    public int insertYwSupplier(YwSupplier ywSupplier)
    {
        if (existData(ywSupplier)) {
            throw new RuntimeException("供应商信息重复");
        }
        return ywSupplierMapper.insertYwSupplier(ywSupplier);
    }

    /**
     * 修改供应商
     * 
     * @param ywSupplier 供应商
     * @return 结果
     */
    @Override
    public int updateYwSupplier(YwSupplier ywSupplier)
    {
        if (existData(ywSupplier)) {
            throw new RuntimeException("供应商信息重复");
        }
        ywSupplier.setUpTime(new Date());
        return ywSupplierMapper.updateYwSupplier(ywSupplier);
    }

    /**
     * 公用方法：验证写入的供应商是否已经存在
     * @param ywSupplier
     * @return
     */
    private boolean existData(YwSupplier ywSupplier) {
        YwSupplier po = new YwSupplier();
        po.setSupplierName(ywSupplier.getSupplierName());
        List<YwSupplier> list = ywSupplierMapper.selectYwSupplierList(po);
        // 默认没有非本数据记录中没有相同名称
        boolean flag = false;
        if(ywSupplier.getIds()!=null){
            // 更新操作
            if(list != null && !list.isEmpty()){
                for (YwSupplier po2 : list) {
                    // 非本数据记录中有相同名称
                    if (!po2.getIds().equals(ywSupplier.getIds())) {
                        flag = true;
                        break;
                    }
                }
            }
        }else {
            // 新增操作
            if(list != null && !list.isEmpty()){
                flag = true;
            }
        }
        return flag;
    }

    /**
     * 批量删除供应商
     * 
     * @param idss 需要删除的供应商主键
     * @return 结果
     */
    @Override
    public int deleteYwSupplierByIdss(Long[] idss)
    {
        return ywSupplierMapper.deleteYwSupplierByIdss(idss);
    }

    /**
     * 删除供应商信息
     * 
     * @param ids 供应商主键
     * @return 结果
     */
    @Override
    public int deleteYwSupplierByIds(Long ids)
    {
        return ywSupplierMapper.deleteYwSupplierByIds(ids);
    }
}
